<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="db.jsp"%>
<%!
    public  static boolean isEmpty(String s){
        return s==null||s.length()==0;
    }
%>
<%
    Object oname=session.getAttribute("name");
    if(oname==null){
        request.setAttribute("msg","请登录后查看");
        request.getRequestDispatcher("login.jsp").forward(request,response);
    }
    request.setCharacterEncoding("UTF-8");
    String sno=request.getParameter("sno");
    String name=request.getParameter("name");
    String sex=request.getParameter("sex");
    String phone=request.getParameter("phone");
    String username=request.getParameter("username");
    String password=request.getParameter("password");
    String clazz=request.getParameter("clazz");
    String homepath=request.getParameter("homepath");

    PreparedStatement pstmt=connection.prepareStatement("insert into t_user values(0,?,?,?,?,?,?,?,?)");
    //4、创建sql语句（下达命令）
    pstmt.setString(1,sno);
    pstmt.setString(2,name);
    pstmt.setString(3,sex);
    pstmt.setString(4,phone);
    pstmt.setString(5,username);
    pstmt.setString(6,password);
    pstmt.setString(7,clazz);
    pstmt.setString(8,homepath);
    try{
        int count=pstmt.executeUpdate();
        out.print("添加成功，2秒后跳转到首页");
        response.setHeader("refresh","2;URL=index.jsp");
    }catch (Exception e){
        request.setAttribute("name",name);
        request.setAttribute("sex",sex);
        request.setAttribute("phone",phone);
        request.setAttribute("username",username);
        request.setAttribute("password",password);
        request.setAttribute("clazz",clazz);
        request.setAttribute("homepath",homepath);
        request.setAttribute("msg",e.getLocalizedMessage());
        request.getRequestDispatcher("add.jsp").forward(request,response);
    }finally {
        connection.close();
    }
%>
